package org.abettor.pushbox.activity;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Random;

import org.abettor.android.ads.YoumiManager;
import org.abettor.pushbox.config.Config;
import org.abettor.pushbox.map.Boxmap;
import org.abettor.pushbox.map.BoxmapUtil;
import org.abettor.pushbox.map.Step;
import org.abettor.pushbox.util.FileUtil;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
/**
 * 显示我解决的方法
 * @author jzh
 *
 */
public class QuesstionBoxShowResultActivity extends AbstractShowResultActivity {
	private int map_id = -1;// 地图的id
	private final int SHOW_AD = 13579;
	private YoumiManager youmi = new YoumiManager(this);
	private Handler youmiHandle = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			if (msg.what == SHOW_AD) {
				// 按照一定的概率显示广告
				Random rand = new Random(System.currentTimeMillis());
				int val = rand.nextInt(99);
				if (val < 100) {
					youmi.showYoumi(YoumiManager.Position.middle, Color.WHITE,
							Color.BLACK, 0xAA);
				}
			}

			super.handleMessage(msg);
		}
	};
	private Thread youmiDelayThread = new Thread() {
		@Override
		public void run() {
			try {
				Thread.sleep(200);
				Message msg = new Message();
				msg.what = SHOW_AD;
				youmiHandle.sendMessage(msg);
			} catch (InterruptedException e) {
				Log.d(this.getClass().getName(), "", e);
			}
		}
	};

	@Override
	protected Boxmap initMap() {
		String folder = Config.getNetBasePath() + File.separator
				+ String.valueOf(map_id) + ".map";
		try {
			byte[] buf = FileUtil.readFileByPath(folder);
			return BoxmapUtil.paraArrayToMap(buf);
		} catch (IOException e) {
			Log.e(this.getClass().getName(), "", e);
		}
		return null;
	}
	@Override
	protected List<Step> initStep() {
		String folder = Config.getNetBasePath() + File.separator
				+ String.valueOf(map_id) + ".step";
		try {
			byte[] buf = FileUtil.readFileByPath(folder);
			return BoxmapUtil.paraArrayToStep(buf);
		} catch (IOException e) {
		}
		return null;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		init();
		super.onCreate(savedInstanceState);
		youmiDelayThread.start();
	}

	private void init() {
		Intent intent = getIntent();
		Bundle extras = intent.getExtras();
		map_id = extras.getInt("_id");// 获得数据的id
	}

	@Override
	public void onBackPressed() {
		super.onBackPressed();
	}

}
